#!/bin/bash
# macOS 网络配置完整恢复脚本

echo "开始恢复网络配置..."

# 检查是否以管理员权限运行
if [ "$EUID" -ne 0 ]; then
    echo "此脚本需要管理员权限，请使用 sudo ./restore.sh"
    exit 1
fi

# 1. 恢复hosts文件
if [ -f "hosts.txt" ]; then
    echo "恢复hosts文件..."
    cp hosts.txt /etc/hosts
    echo "✅ hosts文件已恢复"
else
    echo "❌ 找不到hosts.txt文件"
fi

# 2. 恢复网络偏好设置
if [ -d "SystemConfiguration" ]; then
    echo "恢复网络偏好设置..."
    # 备份当前配置
    cp -r /Library/Preferences/SystemConfiguration /Library/Preferences/SystemConfiguration.backup.$(date +%Y%m%d_%H%M%S)
    
    # 恢复配置
    cp -r SystemConfiguration/* /Library/Preferences/SystemConfiguration/
    echo "✅ 网络偏好设置已恢复"
else
    echo "❌ 找不到SystemConfiguration目录"
fi

# 3. 恢复代理设置
echo "恢复代理设置..."
for proxy_file in proxy_web_*.txt; do
    if [ -f "$proxy_file" ]; then
        # 从文件名提取服务名
        service_name=$(echo "$proxy_file" | sed 's/proxy_web_//' | sed 's/.txt$//' | sed 's/_/ /g')
        echo "恢复 $service_name 的HTTP代理..."
        
        # 解析代理设置
        if grep -q "Enabled: Yes" "$proxy_file"; then
            server=$(grep "Server:" "$proxy_file" | cut -d' ' -f2)
            port=$(grep "Port:" "$proxy_file" | cut -d' ' -f2)
            if [ ! -z "$server" ] && [ ! -z "$port" ]; then
                networksetup -setwebproxy "$service_name" "$server" "$port"
                echo "  设置HTTP代理: $server:$port"
            fi
        else
            networksetup -setwebproxystate "$service_name" off
            echo "  禁用HTTP代理"
        fi
    fi
done

for proxy_file in proxy_secure_*.txt; do
    if [ -f "$proxy_file" ]; then
        service_name=$(echo "$proxy_file" | sed 's/proxy_secure_//' | sed 's/.txt$//' | sed 's/_/ /g')
        echo "恢复 $service_name 的HTTPS代理..."
        
        if grep -q "Enabled: Yes" "$proxy_file"; then
            server=$(grep "Server:" "$proxy_file" | cut -d' ' -f2)
            port=$(grep "Port:" "$proxy_file" | cut -d' ' -f2)
            if [ ! -z "$server" ] && [ ! -z "$port" ]; then
                networksetup -setsecurewebproxy "$service_name" "$server" "$port"
                echo "  设置HTTPS代理: $server:$port"
            fi
        else
            networksetup -setsecurewebproxystate "$service_name" off
            echo "  禁用HTTPS代理"
        fi
    fi
done

for proxy_file in proxy_socks_*.txt; do
    if [ -f "$proxy_file" ]; then
        service_name=$(echo "$proxy_file" | sed 's/proxy_socks_//' | sed 's/.txt$//' | sed 's/_/ /g')
        echo "恢复 $service_name 的SOCKS代理..."
        
        if grep -q "Enabled: Yes" "$proxy_file"; then
            server=$(grep "Server:" "$proxy_file" | cut -d' ' -f2)
            port=$(grep "Port:" "$proxy_file" | cut -d' ' -f2)
            if [ ! -z "$server" ] && [ ! -z "$port" ]; then
                networksetup -setsocksfirewallproxy "$service_name" "$server" "$port"
                echo "  设置SOCKS代理: $server:$port"
            fi
        else
            networksetup -setsocksfirewallproxystate "$service_name" off
            echo "  禁用SOCKS代理"
        fi
    fi
done

# 4. 重启网络服务
echo "重启网络服务..."
dscacheutil -flushcache
killall -HUP mDNSResponder

# 5. 重启网络接口
echo "重启网络接口..."
networksetup -setairportpower en0 off 2>/dev/null
sleep 2
networksetup -setairportpower en0 on 2>/dev/null

echo ""
echo "✅ 网络配置恢复完成！"
echo "建议重启系统以确保所有设置完全生效"
echo "重启后请测试网络连接和Redis服务器访问"
